package com.example.springbootdemo.service.impl;

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams;
import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
import org.apache.poi.ss.usermodel.*;

/**
 * 表头和列表行样式自定义实现类
 * @author hh
 * @date 2022/3/11
 */
public class ExcelExportStylerCustomizeImpl implements IExcelExportStyler {

    private static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT");
    private static final short HEADER_FONT_SIZE = 15;
    private static final short TITLE_FONT_SIZE = 13;
    private static final short DATA_FONT_SIZE = 12;

    /**
     * 大标题样式
     */
    private final CellStyle headerStyle;

    /**
     * 每列标题样式
     */
    private final CellStyle titleStyle;

    /**
     * 数据行样式
     */
    private final CellStyle dataStyle;

    public ExcelExportStylerCustomizeImpl(Workbook workbook) {
        this.headerStyle = initHeaderStyle(workbook);
        this.titleStyle = initTitleStyle(workbook);
        this.dataStyle = initDataStyles(workbook);
    }

    @Override
    public CellStyle getHeaderStyle(short i) {
        return headerStyle;
    }

    @Override
    public CellStyle getTitleStyle(short i) {
        return titleStyle;
    }

    @Override
    public CellStyle getStyles(boolean b, ExcelExportEntity excelExportEntity) {
        return dataStyle;
    }

    @Override
    public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) {
        return getStyles(true, entity);
    }

    @Override
    public CellStyle getTemplateStyles(boolean b, ExcelForEachParams excelForEachParams) {
        return null;
    }

    /**
     * 初始化数据行样式
     * @param workbook workbook
     * @return CellStyle
     */
    private CellStyle initDataStyles(Workbook workbook) {
        CellStyle dataStyle = getBaseCellStyle(workbook);
        dataStyle.setFont(getFont(workbook, DATA_FONT_SIZE, false));
        dataStyle.setDataFormat(STRING_FORMAT);
        return dataStyle;
    }

    /**
     * 初始化标题行样式
     * @param workbook workbook
     * @return CellStyle
     */
    private CellStyle initTitleStyle(Workbook workbook) {
        CellStyle titleStyle = getBaseCellStyle(workbook);
        titleStyle.setFont(getFont(workbook, TITLE_FONT_SIZE, true));
        titleStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return titleStyle;
    }

    /**
     * 初始化表头样式
     * @param workbook workbook
     * @return CellStyle
     */
    private CellStyle initHeaderStyle(Workbook workbook) {
        CellStyle headerStyle = getBaseCellStyle(workbook);
        headerStyle.setFont(getFont(workbook, HEADER_FONT_SIZE, true));
        return headerStyle;
    }

    /**
     * 获取基本的单元格样式
     * @param workbook workbook
     * @return CellStyle
     */
    private CellStyle getBaseCellStyle(Workbook workbook){
        CellStyle style = workbook.createCellStyle();
        /*//下边框
        style.setBorderBottom(BorderStyle.THIN);
        //左边框
        style.setBorderLeft(BorderStyle.THIN);
        //上边框
        style.setBorderTop(BorderStyle.THIN);
        //右边框
        style.setBorderRight(BorderStyle.THIN);*/
        //水平居中
        style.setAlignment(HorizontalAlignment.CENTER);
        //上下居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置自动换行
        style.setWrapText(true);
        return style;
    }

    /**
     * 获取字体
     * @param workbook workbook
     * @param size 字体大小
     * @param isBold 是否粗体
     * @return Font
     */
    private Font getFont(Workbook workbook, short size, boolean isBold) {
        Font font = workbook.createFont();
        //字体样式
        font.setFontName("宋体");
        //是否加粗
        font.setBold(isBold);
        //字体大小
        font.setFontHeightInPoints(size);
        return font;
    }

}
